// admin/system/service/sys_notice_service.go
package service

import (
	"errors"
	repo "go-admin1/admin/system/dao"
	"go-admin1/admin/system/model"
)

// SysNoticeService 通知公告服务接口
type SysNoticeService interface {
	Create(notice *model.SysNotice) error
	GetByID(id int64) (*model.SysNotice, error)
	Update(notice *model.SysNotice) error
	Delete(id int64) error
	List(page, pageSize int) ([]*model.SysNotice, int64, error)
	ListAll() ([]*model.SysNotice, error)
}

type sysNoticeService struct {
	noticeDao repo.SysNoticeDao
}

func (s sysNoticeService) Create(notice *model.SysNotice) error {
	return s.noticeDao.Create(notice)
}

func (s sysNoticeService) GetByID(id int64) (*model.SysNotice, error) {
	notice, err := s.noticeDao.GetByID(id)
	if err != nil {
		return nil, err
	}
	return notice, nil
}

func (s sysNoticeService) Update(notice *model.SysNotice) error {
	existingNotice, err := s.noticeDao.GetByID(notice.NoticeID)
	if err != nil {
		return err
	}
	if existingNotice == nil {
		return errors.New("通知公告不存在")
	}

	return s.noticeDao.Update(notice)
}

func (s sysNoticeService) Delete(id int64) error {
	existingNotice, err := s.noticeDao.GetByID(id)
	if err != nil {
		return err
	}
	if existingNotice == nil {
		return errors.New("通知公告不存在")
	}

	return s.noticeDao.Delete(id)
}

func (s sysNoticeService) List(page, pageSize int) ([]*model.SysNotice, int64, error) {
	notices, total, err := s.noticeDao.List(page, pageSize)
	if err != nil {
		return nil, 0, err
	}
	return notices, total, nil
}

func (s sysNoticeService) ListAll() ([]*model.SysNotice, error) {
	notices, err := s.noticeDao.ListAll()
	if err != nil {
		return nil, err
	}
	return notices, nil
}

// NewSysNoticeService 创建通知公告服务实例
func NewSysNoticeService() SysNoticeService {
	return &sysNoticeService{
		noticeDao: repo.NewSysNoticeDao(),
	}
}
